<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 *
 * @version 1.0
 * @author xpl0it
 */
class Line_service extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('line_service_model');
        $this->load->model('sdp_model');
        $this->load->model('pon_model');
        $this->load->model('customer_model');
       // $this->output->enable_profiler(TRUE);
    }

    function index($sdp_id) {
        $this->checkauth();
        $data['query'] = $this->line_service_model->select_by_sdpid($sdp_id);
        
        $data['sdp'] = $this->sdp_model->select_by_filter($sdp_id);
        $data['pon'] = $this->pon_model->select_by_filter($data['sdp']['pon_id']);

        $content = $this->load->view('line_service/index', $data, true);
        $content_data = new stdClass();
        $content_data->content = $content;
        $this->load->view('template/render_dt', $content_data);
    }

    function install(){
        $this->checkauth();
        $data['query'] = $this->line_service_model->select_by_view();

       $content = $this->load->view('line_service/install', $data, true);
        $content_data = new stdClass();
        $content_data->content = $content;
        $this->load->view('template/render_dt', $content_data); 
    }
    function add($sdp_id) {
        $this->checkauth();
        $data['sdp'] = $this->sdp_model->select_by_filter($sdp_id);
        $data['pon'] = $this->pon_model->select_by_filter($data['sdp']['pon_id']);
        $data['customer'] =  $this->customer_model->select_by_table('customer');

        $content = $this->load->view('line_service/form', $data, true);
        $content_data = new stdClass();
        $content_data->content = $content;
        $this->load->view('template/render', $content_data);
    }

    function edit($sdp_id, $param) {
        $this->checkauth();
        $data['sdp'] = $this->sdp_model->select_by_filter($sdp_id);
        $data['pon'] = $this->pon_model->select_by_filter($data['sdp']['pon_id']);
        
        $data['val'] = $this->line_service_model->select_by_filter($param);
        $data['customer'] =  $this->customer_model->select_by_table('customer');
        
        $content = $this->load->view('line_service/form', $data, true);
        $content_data = new stdClass();
        $content_data->content = $content;
        $this->load->view('template/render', $content_data);
    }

    function save() {
        $this->checkauth();

        if ($_POST['ls_id'] != '') {
            $rs = $this->line_service_model->update($this->input->post(NULL, TRUE));
            if ($rs) {
                $data['ls_id'] = $_POST['ls_id'];

                $this->load->view('template/header');
                $data = '<script type="text/javascript">alert("แก้ไขข้อมูลสำเร็จแล้ว !");location = "' . base_url("index.php/admin/line_service/index/{$_POST['sdp_id']}") . '";</script>';
                echo $data;
            } else {
                $this->load->view('template/header');
                $data = '<script type="text/javascript">alert("ข้อมูลไม่ถูกต้อง ไม่สามารถแก้ไขข้อมูลได้ !");window.history.back();</script>';
                echo $data;
            }
        } else {
            $rs = $this->line_service_model->insert($this->input->post(NULL, TRUE));
            if ($rs) {
                $data['ls_id'] = $rs;

                $this->load->view('template/header');
                $data = '<script type="text/javascript">alert("บันทึกข้อมูลสำเร็จแล้ว !");location = "' . base_url("index.php/admin/line_service/index/{$_POST['sdp_id']}") . '";</script>';
                echo $data;
            } else {
                $this->load->view('template/header');
                $data = '<script type="text/javascript">alert("ข้อมูลไม่ถูกต้อง ไม่สามารถบันทึกข้อมูลได้ !");window.history.back();</script>';
                echo $data;
            }
        }
    }

    function del($id) {
        $this->checkauth();
        $rs = $this->line_service_model->delete($id);
        if ($rs) {
            $this->load->view('template/header');
            $data = '<script type="text/javascript">alert("ลบข้อมูล Line Service สำเร็จแล้ว ! ");window.history.back();</script>';
            echo $data;
        }
    }
    
    function installation(){
        $this->checkauth();
        $data = $this->input->post(NULL, TRUE);
        $rs = $this->line_service_model->update_equip($data);
        if($rs){
                $this->load->view('template/header');
                $data = '<script type="text/javascript">alert("บันทึกข้อมูลสำเร็จแล้ว !");location = "' . base_url("index.php/admin/line_service/install") . '";</script>';
                echo $data;
        } else {
                $this->load->view('template/header');
                $data = '<script type="text/javascript">alert("ข้อมูลไม่ถูกต้อง ไม่สามารถบันทึกข้อมูลได้ !");window.history.back();</script>';
                echo $data;

        }
    }
    
    function check_install($code,$lineid){
        $this->checkauth();
        $data=array(
           'cm_code'=>$code,
           'cm_lineid'=>$lineid
        );
        $rs = $this->line_service_model->check_install_line($data);
        if($rs){
            foreach ($rs as $r){
            $val = $r->line_eq_stat_id;
            }
        $str = "<td> <input type='text'  name='pon_eq_date' class='datepicker' value=".$val."> </td>";    

        } else{
       $str = "<td> <input type='text'  name='pon_eq_date' class='datepicker' placeholder='วันที่ดำเนินการติดตั้ง' readonly required> </td>";

        }

        
        return $str;
    }
    private function checkauth() {
        if (!$this->session->userdata('authen')) {
            redirect('admin/login/', 'refresh');
        }
    }

}

/* End of file customer.php */
/* Location: ./application/controllers/admin/line_service.php */